#!/Users/timmenzies/opt/gawk/bin/gawk -f 
 
# quartile.awk
# Revision: 296
# Built by timmenzies on Fri Jul 29 20:43:58 PDT 2011.

#   _ _ _                                               ((
#  |=|=|=|.----------------------------------------.    _))_
#  |-|-|-|| AWK code of a tiny book on data mining | ___\__/)_
#  |_|_|_|'----------------------------------------| |_|_~~_|_|

# This file is part of TINY.
#
# TINY is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# TINY is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
#  along with TINY.  If not, see <http://www.gnu.org/licenses/>.

 @include "lib.awk"
 function Quartile(all,  a,n){
	resetSeed(1) 	
	n=100
	a[1]=rand()
 	while(n--) push(a,rand())
 	print quartile(a,"f=100;F=%5.2f;Shrink=2")
 }
 function quartile(s0, control,\
                       o,s,n,min,q1,median,q3,max,kludge,c) {
  p2a("f=1;F=%5.1f;Low=-;High=+;Median=|;Max=];Min=[;"\
       "Shrink=2;Left=-100;Right=100;Off= ;Prefix=;" control,o, "[;=]")
  n     = asort(s0,s)
  min   = s[1];
  q1    = s[int(n/4)];
  median= s[int(2*n/4)];
  q3    = s[int(3*n/4)];
  max   = s[n];
  c     = ","
  f     = o["f"]
  return sprintf(o["Prefix"] o["F"] c o["F"] c o["F"] c o["F"] c o["F"] ",%s",
                min,q1,median,q3,max,
                quartile1(o,min*f,q1*f,median*f,q3*f,max*f,o["Right"],o["Shrink"])) 
 }
 function quartile1(o,min,q1,median,q3,max,width, scale,    i,l,str) {
        width  /= scale
        min    /= scale
        q1     /= scale
        median /= scale
        q3     /= scale
        max    /= scale
        for(i=  1; i<=width; i++) l[int(i)]= o["Off"];
        for(i=min;    i<=q1; i++) l[int(i)]= o["Low"]
        for(i= q3;   i<=max; i++) l[int(i)]= o["High"]
        l[int(median)] = o["Median"]
        for(i= 1; i<=width; i++) str = str l[int(i)];
        return o["Min"] str o["Max"]
 }

